package cn.kgc.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.kgc.mes.common.annotation.Log;
import cn.kgc.mes.common.core.controller.BaseController;
import cn.kgc.mes.common.core.domain.AjaxResult;
import cn.kgc.mes.common.enums.BusinessType;
import cn.kgc.mes.domain.MachineryType;
import cn.kgc.mes.service.IMachineryTypeService;
import cn.kgc.mes.common.utils.poi.ExcelUtil;
import cn.kgc.mes.common.core.page.TableDataInfo;

/**
 * 设备类型设置Controller
 * 
 * @author xxf
 * @date 2025-09-10
 */
@RestController
@RequestMapping("/deviceType/deviceType")
public class MachineryTypeController extends BaseController
{
    @Autowired
    private IMachineryTypeService machineryTypeService;

    /**
     * 查询设备类型设置列表
     */
//    @PreAuthorize("@ss.hasPermi('deviceType:deviceType:list')")
    @GetMapping("/list")
    public TableDataInfo list(MachineryType machineryType)
    {
        startPage();
        List<MachineryType> list = machineryTypeService.selectMachineryTypeList(machineryType);
        return getDataTable(list);
    }

    /**
     * 导出设备类型设置列表
     */
//    @PreAuthorize("@ss.hasPermi('deviceType:deviceType:export')")
    @Log(title = "设备类型设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MachineryType machineryType)
    {
        List<MachineryType> list = machineryTypeService.selectMachineryTypeList(machineryType);
        ExcelUtil<MachineryType> util = new ExcelUtil<MachineryType>(MachineryType.class);
        util.exportExcel(response, list, "设备类型设置数据");
    }

    /**
     * 获取设备类型设置详细信息
     */
//    @PreAuthorize("@ss.hasPermi('deviceType:deviceType:query')")
    @GetMapping(value = "/{machineryTypeId}")
    public AjaxResult getInfo(@PathVariable("machineryTypeId") Long machineryTypeId)
    {
        return success(machineryTypeService.selectMachineryTypeByMachineryTypeId(machineryTypeId));
    }

    /**
     * 新增设备类型设置
     */
//    @PreAuthorize("@ss.hasPermi('deviceType:deviceType:add')")
    @Log(title = "设备类型设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MachineryType machineryType)
    {
        return toAjax(machineryTypeService.insertMachineryType(machineryType));
    }

    /**
     * 修改设备类型设置
     */
//    @PreAuthorize("@ss.hasPermi('deviceType:deviceType:edit')")
    @Log(title = "设备类型设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MachineryType machineryType)
    {
        return toAjax(machineryTypeService.updateMachineryType(machineryType));
    }

    /**
     * 删除设备类型设置
     */
//    @PreAuthorize("@ss.hasPermi('deviceType:deviceType:remove')")
    @Log(title = "设备类型设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{machineryTypeIds}")
    public AjaxResult remove(@PathVariable Long[] machineryTypeIds)
    {
        return toAjax(machineryTypeService.deleteMachineryTypeByMachineryTypeIds(machineryTypeIds));
    }
    /**
     * 查询设备类型树
     */
//    @PreAuthorize("@ss.hasPermi('deviceType:deviceType:list')")
    @GetMapping("/tree")
    public AjaxResult tree(MachineryType machineryType)
    {
        List<MachineryType> list = machineryTypeService.selectMachineryTypeList(machineryType);
        List<MachineryType> tree = buildTree(list);
        return success(tree);
    }

    /**
     * 构建树形结构
     */
    private List<MachineryType> buildTree(List<MachineryType> list) {
        Map<Long, MachineryType> map = new HashMap<>();
        List<MachineryType> roots = new ArrayList<>();

        // 先放到map
        for (MachineryType item : list) {
            map.put(item.getMachineryTypeId(), item);
            item.setChildren(new ArrayList<>());
        }

        // 构建父子关系
        for (MachineryType item : list) {
            Long parentId = item.getParentTypeId(); // 确认你表里字段是 parent_type_id
            if (parentId == null || parentId == 0) {
                roots.add(item);
            } else {
                MachineryType parent = map.get(parentId);
                if (parent != null) {
                    parent.getChildren().add(item);
                } else {
                    roots.add(item); // 没找到父节点，兜底当根
                }
            }
        }
        return roots;
    }
}
